package cn.lg.soar.flow.biz.listener;

import cn.lg.soar.common.util.current.CurrentProxy;
import cn.lg.soar.flow.api.enums.ApproverMissingEnum;
import cn.lg.soar.flow.api.model.ApprovalTaskDTO;
import cn.lg.soar.flow.api.model.node.ApprovalNode;
import cn.lg.soar.flow.api.utils.ProcessUtils;
import cn.lg.soar.flow.biz.model.PassTaskDTO;
import cn.lg.soar.flow.biz.model.RejectTaskDTO;
import cn.lg.soar.flow.biz.service.FlowInstanceService;
import cn.lg.soar.flow.biz.service.SoarProcessVersionService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Slf4j
@Component("innerTaskListener")
public class InnerTaskListener implements TaskListener, CurrentProxy<InnerTaskListener> {

    @Autowired
    private SoarProcessVersionService processVersionService;
    @Autowired
    private FlowInstanceService flowInstanceService;

    @Override
    public void notify(DelegateTask delegateTask) {
        Set<IdentityLink> candidates = delegateTask.getCandidates();
        if (candidates.size() == 1 && candidates.stream().map(IdentityLink::getUserId).collect(Collectors.toSet()).contains(ProcessUtils.SYSTEM_APPROVAL)) {
            // 有且仅有一个系统候选人的是审批人缺失，自动完成任务
            Map<String, ApprovalNode> nodeMap = processVersionService.getNodeMap(delegateTask.getProcessDefinitionId());
            ApprovalNode node = nodeMap.get(delegateTask.getTaskDefinitionKey());

            ApprovalTaskDTO taskDTO = new ApprovalTaskDTO();
            taskDTO.setTaskId(delegateTask.getId());
            taskDTO.setTaskKey(delegateTask.getTaskDefinitionKey());
            taskDTO.setProcessId(delegateTask.getProcessDefinitionId());
            List<ApprovalTaskDTO> tasks = List.of(taskDTO);

            if (ApproverMissingEnum.pass.equals(node.getMissingAssignee())) {
                PassTaskDTO passTaskDTO = new PassTaskDTO();
                passTaskDTO.setComments("【系统自动】审批人缺失，自动通过");
                passTaskDTO.setTasks(tasks);
                passTaskDTO.setUserId(ProcessUtils.SYSTEM_USER_ID);
                flowInstanceService.passTask(passTaskDTO);
            } else {
                RejectTaskDTO rejectTaskDTO = new RejectTaskDTO();
                rejectTaskDTO.setComments("【系统自动】审批人缺失，自动拒绝");
                rejectTaskDTO.setTasks(tasks);
                rejectTaskDTO.setUserId(ProcessUtils.SYSTEM_USER_ID);
                rejectTaskDTO.setProcInstId(delegateTask.getProcessInstanceId());
                rejectTaskDTO.setBackNodeKey("apply");
                flowInstanceService.rejectTask(rejectTaskDTO);
            }
        }
    }

}